﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using TouchRC.Classes;
using TouchRC.Logic;
using System.Drawing;
using TouchRC.Themes;
using TouchRC.BarManagers;
using System.IO;

namespace TouchRC
{
    public class ComputerConfigPanel : FluidPanelBase
    {
        #region Private Members

        private ComputerListBoxItem m_Computer = new ComputerListBoxItem();
        private ComputerConfigState m_State;

        #region Fluid Controls

        private FluidLabel m_NameLabel;
        private FluidLabel m_AddressLabel;
        private FluidLabel m_ReceiverPortLabel;
        private FluidLabel m_ControllerPortLabel;
        private FluidLabel m_CacheDirLabel;

        private FluidTextBox m_NameTextBox;
        private FluidTextBox m_AddressTextBox;
        private FluidTextBox m_ReceiverPortTextBox;
        private FluidTextBox m_ControllerPortTextBox;
        private FluidTextBox m_CacheDirTextBox;

        private FluidButton m_SaveButton;
        private FluidButton m_CancelButton;

        #endregion

        #endregion

        public override void AddControls()
        {
            Controls.Add(m_AddressLabel);
            Controls.Add(m_NameLabel);
            Controls.Add(m_ReceiverPortLabel);
            Controls.Add(m_ControllerPortLabel);
            Controls.Add(m_CacheDirLabel);

            Controls.Add(m_AddressTextBox);
            Controls.Add(m_NameTextBox);
            Controls.Add(m_ReceiverPortTextBox);
            Controls.Add(m_ControllerPortTextBox);
            Controls.Add(m_CacheDirTextBox);

            Controls.Add(m_SaveButton);
            Controls.Add(m_CancelButton);
        }

        public override void SetControlPositions(ScreenOrientation screenOrientation)
        {

            int labelWidth = ScaleX(100);
            int leftSpacing = ScaleX(10);
            int textBoxStringWidth = ScreenWidth - labelWidth - (2 * leftSpacing);
            int textBoxIntWidth = ScaleX(50);

            const int namePos = 1;
            const int addressPos = 2;
            const int controlPos = 3;
            const int receivePos = 4;
            const int cacheDirPos = 5;

            #region Labels

            m_NameLabel.Bounds = new Rectangle(leftSpacing, GetYPos(namePos, false), labelWidth, BaseControlHeight);
            m_AddressLabel.Bounds = new Rectangle(leftSpacing, GetYPos(addressPos, false), labelWidth, BaseControlHeight);
            m_ControllerPortLabel.Bounds = new Rectangle(leftSpacing, GetYPos(controlPos, false), labelWidth, BaseControlHeight);
            m_ReceiverPortLabel.Bounds = new Rectangle(leftSpacing, GetYPos(receivePos, false), labelWidth, BaseControlHeight);
            m_CacheDirLabel.Bounds = new Rectangle(leftSpacing, GetYPos(cacheDirPos, false), labelWidth, BaseControlHeight);

            #endregion

            #region Textboxes

            m_NameTextBox.Bounds = new Rectangle(ScreenWidth - textBoxStringWidth - leftSpacing, GetYPos(namePos, true), textBoxStringWidth, BaseControlHeight);
            m_AddressTextBox.Bounds = new Rectangle(ScreenWidth - textBoxStringWidth - leftSpacing, GetYPos(addressPos, true), textBoxStringWidth, BaseControlHeight);
            m_ControllerPortTextBox.Bounds = new Rectangle(ScreenWidth - textBoxIntWidth - leftSpacing, GetYPos(controlPos, true), textBoxIntWidth, BaseControlHeight);
            m_ReceiverPortTextBox.Bounds = new Rectangle(ScreenWidth - textBoxIntWidth - leftSpacing, GetYPos(receivePos, true), textBoxIntWidth, BaseControlHeight);
            m_CacheDirTextBox.Bounds = new Rectangle(ScreenWidth - textBoxStringWidth - leftSpacing, GetYPos(cacheDirPos, true), textBoxStringWidth, BaseControlHeight);

            #endregion

            #region Buttons

            m_SaveButton.Bounds = new Rectangle(0, ScreenHeight - ButtonDimensions, ButtonDimensions, ButtonDimensions);
            m_CancelButton.Bounds = new Rectangle(ScreenWidth - ButtonDimensions, ScreenHeight - ButtonDimensions, ButtonDimensions, ButtonDimensions);

            #endregion

            m_CancelButton.Anchor = AnchorBR;
            m_SaveButton.Anchor = AnchorBL;
        }

        public override void InitPanelControls()
        {
            #region Labels

            m_NameLabel = new FluidLabel();
            m_AddressLabel = new FluidLabel();
            m_ControllerPortLabel = new FluidLabel();
            m_ReceiverPortLabel = new FluidLabel();
            m_CacheDirLabel = new FluidLabel();

            #endregion

            #region Textboxes

            m_NameTextBox = new FluidTextBox(string.Empty, 0, 0, 0, 0);
            m_AddressTextBox = new FluidTextBox(string.Empty, 0, 0, 0, 0);
            m_ControllerPortTextBox = new FluidTextBox(string.Empty, 0, 0, 0, 0);
            m_ReceiverPortTextBox = new FluidTextBox(string.Empty, 0, 0, 0, 0);
            m_CacheDirTextBox = new FluidTextBox(string.Empty, 0, 0, 0, 0);

            #endregion

            #region Buttons

            m_SaveButton = new FluidButton();
            m_CancelButton = new FluidButton();

            #endregion
        }

        public override void RefreshValues()
        {
            m_ReceiverPortTextBox.Text = m_Computer.ListeningPort.ToString();
            m_NameTextBox.Text = m_Computer.Name;
            m_AddressTextBox.Text = m_Computer.DnsAddress;
            m_ControllerPortTextBox.Text = m_Computer.RemotePort.ToString();
            m_CacheDirTextBox.Text = m_Computer.MetadataCacheDir == null ? string.Empty : m_Computer.MetadataCacheDir.FullName;
        }

        public override List<BarOption> GetBarOptions()
        {
            List<BarOption> options = new List<BarOption>();
            return options;
        }

        public override void Back()
        {
        }

        protected override void InitControl()
        {
            ShowMenuButton = false;
            EnableRemoteControlGesture = false;
            EnableNowPlayingGesture = false;

            base.InitControl();

            Font labelFont = Theme.CurrentTheme.StandardFont;

            #region Labels

            m_NameLabel.LineAlignment = StringAlignment.Near;
            m_NameLabel.Font = labelFont;
            m_NameLabel.Text = "Name: ";
            m_NameLabel.ForeColor = Color.White;

            m_AddressLabel.LineAlignment = StringAlignment.Near;
            m_AddressLabel.Font = labelFont;
            m_AddressLabel.Text = "Address: ";
            m_AddressLabel.ForeColor = Color.White;

            m_ControllerPortLabel.LineAlignment = StringAlignment.Near;
            m_ControllerPortLabel.Font = labelFont;
            m_ControllerPortLabel.Text = "Controlling Port: ";
            m_ControllerPortLabel.ForeColor = Color.White;

            m_CacheDirLabel.LineAlignment = StringAlignment.Near;
            m_CacheDirLabel.Font = labelFont;
            m_CacheDirLabel.Text = "Cache Directory: ";
            m_CacheDirLabel.ForeColor = Color.White;

            m_ReceiverPortLabel.LineAlignment = StringAlignment.Near;
            m_ReceiverPortLabel.Font = labelFont;
            m_ReceiverPortLabel.Text = "Receiving Port: ";
            m_ReceiverPortLabel.ForeColor = Color.White;

            #endregion

            #region TextBoxes

            m_ReceiverPortTextBox.LineAlignment = StringAlignment.Far;
            m_ReceiverPortTextBox.Font = labelFont;

            m_NameTextBox.LineAlignment = StringAlignment.Far;
            m_NameTextBox.Font = labelFont;

            m_AddressTextBox.LineAlignment = StringAlignment.Far;
            m_AddressTextBox.Font = labelFont;

            m_ControllerPortTextBox.LineAlignment = StringAlignment.Far;
            m_ControllerPortTextBox.Font = labelFont;

            m_CacheDirTextBox.LineAlignment = StringAlignment.Far;
            m_CacheDirTextBox.Font = labelFont;

            #endregion

            #region Buttons

            m_SaveButton.BackColor = Color.Empty;
            m_SaveButton.Shape = ButtonShape.Flat;
            m_SaveButton.Image = ImageFactory.GetBitmap(ImageTypes.Tick);
            m_SaveButton.Click += new EventHandler(m_SaveButton_Click);

            m_CancelButton.BackColor = Color.Empty;
            m_CancelButton.Shape = ButtonShape.Flat;
            m_CancelButton.Image = ImageFactory.GetBitmap(ImageTypes.Cross);
            m_CancelButton.Click += new EventHandler(m_CancelButton_Click);

            #endregion

            m_CancelButton.Anchor = AnchorBR;
            m_SaveButton.Anchor = AnchorBL;
        }

        void m_CancelButton_Click(object sender, EventArgs e)
        {
            GoBackToComputerSelectionScreen();
        }

        void m_SaveButton_Click(object sender, EventArgs e)
        {
            ComputerListBoxItem compy = new ComputerListBoxItem();
            compy.DnsAddress = m_AddressTextBox.Text;
            compy.Name = m_NameTextBox.Text;
            compy.ListeningPort = Convert.ToInt32(m_ReceiverPortTextBox.Text);
            compy.RemotePort = Convert.ToInt32(m_ControllerPortTextBox.Text);
            try
            {
                compy.MetadataCacheDir = new DirectoryInfo(m_CacheDirTextBox.Text);
            }
            catch
            {
            }

            if (m_State == ComputerConfigState.New)
            {
                ConfigSettings.AddComputerConfigs(compy);
            }
            else if (m_State == ComputerConfigState.Edit)
            {
                compy.Id = m_Computer.Id;
                ConfigSettings.UpdateComputerConfigs(compy);
            }

            GoBackToComputerSelectionScreen();
        }

        public override void Show(ShowTransition transition)
        {
            base.Show(transition);
            RefreshValues();
        }
        
        public ComputerConfigPanel(ComputerConfigState state)
        {
            m_State = state;
            switch (state)
            {
                case ComputerConfigState.Edit:
                    {
                        HeaderText = "Edit Computer";
                        break;
                    }
                case ComputerConfigState.New:
                    {
                        HeaderText = "Add Computer";
                        break;
                    }
            }
        }

        #region Public Accessors

        public ComputerListBoxItem Computer
        {
            set
            {
                if (m_State == ComputerConfigState.Edit)
                {
                    m_Computer = value;
                }
            }
        }

        #endregion

        private int GetYPos(int rank, bool isTextBox)
        {
            int retVal = 0;

            

            int headerSpacing = BaseControlHeight;
            int spacer = ScaleX(5);

            retVal = headerSpacing + ((rank - 1) * (BaseControlHeight + spacer));
            
            return retVal;
        }

        private void GoBackToComputerSelectionScreen()
        {
            ComputerSelectionPanel.Instance.RefreshValues();
            this.Close(ShowTransition.FromTop);
        }
    }
}
